/*******************************************************************************
    Universidad del Valle de Guatemala
    Algoritmos y estructuras de datos
    Karen Tojin   08091
    Fecha: 15 de Agosto de 2009
    Descricion: Programa que crea un árbol de Huffman.
***************************************************************************** */

/**
 *
 * @author karenAndy
 */
import java.util.ArrayList;

public class SortedList {
    private ArrayList<TreeNode> sortedList;

    //SortedList constructor
    public SortedList(){
        this.sortedList=new ArrayList<TreeNode>();
    }

    //Method to add another node to the list
    public void Push(TreeNode newNode){
        int listLength=sortedList.size();
        //If empty, adds the 1st node
        if(this.Empty())
            this.sortedList.add(newNode);
        else{
            for(int i=0;i<this.sortedList.size();i++){
                if(newNode.getFrequency()<this.sortedList.get(i).getFrequency()){
                    this.sortedList.add(i,newNode);
                    break;
                }
            }
            if(listLength==this.sortedList.size())
                this.sortedList.add(newNode);
        }
    }

    //Method to remove a node from the list
    public TreeNode Remove(){
        return this.sortedList.remove(0);
    }

    //Method to check id the lisf is empty or not
    public boolean Empty() {
        //post: devuelve true si la lista está vacía
        if(sortedList.size()==0)
            return true;
        //assert this.Empty();
        return false;
    }

    //Method that returns the size of the list
    public int Size(){
        return this.sortedList.size();
    }
   
    //Method the returns the entire list
    public ArrayList<TreeNode> getList(){
        return sortedList;
    }

    //toStrig method
    @Override
    public String toString(){

        String temp = "Sorted List\n";
        for (int i= 0; i < this.sortedList.size() ; i++){
            temp += "Character: "+this.sortedList.get(i).toString()+"\tFrequency: "+this.sortedList.get(i).getFrequency();
        }
        return temp;
   }
}
